package com.wwwarehouse.taskcenter.fragment.job_point;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5HavedUnitResponseBean;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStepGetbeforeResponseBean;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointTypeResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.JobSaveConstant;
import com.wwwarehouse.taskcenter.eventbus_event.TaskPointBackEvent;
import com.wwwarehouse.taskcenter.eventbus_event.TaskPointCardEvent;
import com.wwwarehouse.taskcenter.fragment.job_point.step1.AddJobPointNameFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.child.ChildJobPointDimensionListFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.child.ChooseChildJobPointTypeFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step3.ChooseConnectPersonFragment3;
import com.wwwarehouse.taskcenter.fragment.job_point.step4.ChooseConnectBluetoothFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step4.ChooseJobUnitLookmesspagerFragment4;
import com.wwwarehouse.taskcenter.fragment.job_point.step5.ChooseJobUnitAddFirstpageFragment5;
import com.wwwarehouse.taskcenter.fragment.job_point.step5.ChooseJobUnitHavedUnitPagerFragment5;
import com.wwwarehouse.taskcenter.fragment.job_point.step6.ChooseConnectObjectFragment6;
import com.wwwarehouse.taskcenter.fragment.job_point.step7.SetJobPointRuleFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/24 13:25
 * @描述 新增作业点  拼卡页面
 */

@Route(path = TaskCenterConstant.AddJobPointCardFragment)
public class AddJobPointCardFragment extends BaseFragment implements View.OnClickListener {
    private View mView;

    private RelativeLayout mRlStep1;
    private RelativeLayout mRlStep2;
    private RelativeLayout mRlStep3;
    private RelativeLayout mRlStep4;
    private RelativeLayout mRlStep5;
    private RelativeLayout mRlStep6;
    private RelativeLayout mRlStep7;
    private TextView mTvStep1;
    private TextView mTvStep2;
    private TextView mTvStep3;
    private TextView mTvStep4;
    private TextView mTvStep5;
    private TextView mTvStep6;
    private TextView mTvStep7;
    private TextView mTvStepDes1;
    private TextView mTvStepDes2;
    private TextView mTvStepDes3;
    private TextView mTvStepDes4;
    private TextView mTvStepDes5;
    private TextView mTvStepDes6;
    private TextView mTvStepDes7;
    private ImageView mIvStep1;
    private ImageView mIvStep2;
    private ImageView mIvStep3;
    private ImageView mIvStep4;
    private ImageView mIvStep5;
    private ImageView mIvStep6;
    private ImageView mIvStep7;

    private StateLayout mStateLayout;
    private ElasticScrollView mElasticScrollView;

    private JobSaveConstant mJobSaveConstant;   //卡片之间保存的参数

    private static final int REQUEST_STEP5 = 100003, REQUEST_ALL_JOBPOINT = 100004, REQUEST_ALL_GETBEFORE = 100005;
    private static final int REQUEST_GET_JOB_POINT_TYPE = 100006;

    // 第一步所需的参数
    public static final String KEY_BUSINESS_ID = "key_business_id";   //业务单位id
    // 第二步所需的参数
    public static final String KEY_IS_PARENT = "key_is_parent"; //是否是父作业点
    public static final String KEY_IS_FROM_CARD = "key_is_from_card";  //标记是否从拼卡界面直接进入维度列表界面
    public static final String KEY_JOB_POINT_UKID = "key_job_point_ukid";
    public static final String KEY_JOB_POINT_TYPE = "key_job_point_type";
    public static final String KEY_WAREHOUSE_UKID = "key_warehouse_ukid";
    public static final String KEY_WAREHOUSE_NAME = "key_warehouse_name";

    private String businessId;
    private boolean isParent;
    private boolean isFromCard;
    private String jobPointUkid;
    private String jobPointType;
    private String warehouseUkid;
    private String warehouseName;

    private boolean isStep1Userful = false, isStep2Userful = false, isStep3Userful = false, isStep4Userful = false, isStep5Userful = false, isStep6Userful = false, isStep7Userful = false;
    private boolean isStepAllDone = false;  //是否所有步骤都已经做完
    private String operationUkid;

    private AddJobPointStepGetbeforeResponseBean beforeServerBean;
    private CardDeskTaskResponseBean.TaskBean taskResponseBean;
    private CardDeskFunctionResponseBean.TaskBean functionResponseBean;

    private static final int USERFUL = 0, USELESS = -1, DONE = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_add_job_point_card, container, false);
        initView();
        initData();
        initEvent();
        return mView;
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_layout);

        mElasticScrollView = findView(mView, R.id.esv_elastic);

        mRlStep1 = findView(mView, R.id.rl_step1);
        mRlStep2 = findView(mView, R.id.rl_step2);
        mRlStep3 = findView(mView, R.id.rl_step3);
        mRlStep4 = findView(mView, R.id.rl_step4);
        mRlStep5 = findView(mView, R.id.rl_step5);
        mRlStep6 = findView(mView, R.id.rl_step6);
        mRlStep7 = findView(mView, R.id.rl_step7);

        mTvStep1 = findView(mView, R.id.tv_step1);
        mTvStep2 = findView(mView, R.id.tv_step2);
        mTvStep3 = findView(mView, R.id.tv_step3);
        mTvStep4 = findView(mView, R.id.tv_step4);
        mTvStep5 = findView(mView, R.id.tv_step5);
        mTvStep6 = findView(mView, R.id.tv_step6);
        mTvStep7 = findView(mView, R.id.tv_step7);

        mTvStepDes1 = findView(mView, R.id.tv_step_des1);
        mTvStepDes2 = findView(mView, R.id.tv_step_des2);
        mTvStepDes3 = findView(mView, R.id.tv_step_des3);
        mTvStepDes4 = findView(mView, R.id.tv_step_des4);
        mTvStepDes5 = findView(mView, R.id.tv_step_des5);
        mTvStepDes6 = findView(mView, R.id.tv_step_des6);
        mTvStepDes7 = findView(mView, R.id.tv_step_des7);

        mIvStep1 = (ImageView) mView.findViewById(R.id.iv_step1);
        mIvStep2 = (ImageView) mView.findViewById(R.id.iv_step2);
        mIvStep3 = (ImageView) mView.findViewById(R.id.iv_step3);
        mIvStep4 = (ImageView) mView.findViewById(R.id.iv_step4);
        mIvStep5 = (ImageView) mView.findViewById(R.id.iv_step5);
        mIvStep6 = (ImageView) mView.findViewById(R.id.iv_step6);
        mIvStep7 = (ImageView) mView.findViewById(R.id.iv_step7);

//        setStep7Enable(USERFUL);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            taskResponseBean = (CardDeskTaskResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS);
            functionResponseBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
        }

        if (taskResponseBean == null && functionResponseBean != null) {
            // 从功能卡片进来的时候，每次都新建，不需要请求接口
        } else {
            // 从任务卡片进来的时候，需要请求接口
            mStateLayout.showProgressView(false);
        }

        mJobSaveConstant = new JobSaveConstant();
    }

    private void initEvent() {
        mElasticScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
            @Override
            public void onDownPull() {
                ((BaseCardDeskActivity) mActivity).setTitleBt1Text("启用");

                ((BaseCardDeskActivity) mActivity).showTitleBt1();
                ((BaseCardDeskActivity) mActivity).downPull_3();

//                if (isStep6Userful) { //第五步完成就可以下拉完成了
                if (isStep7Userful || isStepAllDone) { //第六步必选，第七步可选，第六步做完或者第七步做完 就可以下拉完成了
                    ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);
                } else {
                    ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(false);
                }
            }

            @Override
            public void onUpPull() {

            }
        });

        mRlStep1.setOnClickListener(this);
        mRlStep2.setOnClickListener(this);
        mRlStep3.setOnClickListener(this);
        mRlStep4.setOnClickListener(this);
        mRlStep5.setOnClickListener(this);
        mRlStep6.setOnClickListener(this);
        mRlStep7.setOnClickListener(this);

        setStep1Enable(USELESS);
    }

    @Override
    public void titleBt1Click() {
        if (peekFragment() instanceof AddJobPointCardFragment) {
            startJobPoint();
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof AddJobPointCardFragment) {
            mActivity.setTitle("" + getString(R.string.new_task_newpoint));
        }
    }

    @Override
    public void requestDatas() {
        // 进入拼卡页面，需要调用继续上次未完成的任务接口
        if (taskResponseBean == null && functionResponseBean != null) { //任务为空了 说明从功能的卡片进来的了
            businessId = functionResponseBean.getBusinessId();
            BaseApplication.sp.putValue(TaskCenterConstant.CONSTANT_BUSINESSID, functionResponseBean.getBusinessId());
            setStep1Enable(USERFUL);
        } else {
            businessId = taskResponseBean.getBelongBusiness();
            BaseApplication.sp.putValue(TaskCenterConstant.CONSTANT_BUSINESSID, taskResponseBean.getBelongBusiness());
            operationUkid = taskResponseBean.getCardUkid();

            getBeforeStep();
        }
    }

    @Override
    public void onClick(View v) {
        JobSaveConstant.Step1 step1 = mJobSaveConstant.getStep1();

        if (step1 != null) {
            isParent = step1.isParentJobPoint();
            jobPointUkid = step1.getJobPointUkid();
            warehouseUkid = step1.getWarehouseUkid();
            warehouseName = step1.getWarehouseName();
            BaseApplication.sp.putValue("warehouseUkid", warehouseUkid);
        }

        int id = v.getId();
        if (id == R.id.rl_step1 && isStep1Userful) {
            AddJobPointNameFragment addJobPointNameFragment = new AddJobPointNameFragment();
            Bundle bundle = new Bundle();
            bundle.putString(KEY_BUSINESS_ID, businessId);
            addJobPointNameFragment.setArguments(bundle);
            pushFragment(addJobPointNameFragment);
        } else if (id == R.id.rl_step2 && isStep2Userful) {
            //新增作业点
            if (isFromCard) {
                // 已经保存过维度，直接跳转到维度列表
                Bundle bundle = new Bundle();

                bundle.putString(KEY_BUSINESS_ID, businessId);
                bundle.putBoolean(KEY_IS_PARENT, isParent);
                bundle.putBoolean(KEY_IS_FROM_CARD, isFromCard);
                bundle.putString(KEY_JOB_POINT_UKID, jobPointUkid);
                bundle.putString(KEY_JOB_POINT_TYPE, jobPointType);
                bundle.putString(KEY_WAREHOUSE_UKID, warehouseUkid);
                bundle.putString(KEY_WAREHOUSE_NAME, warehouseName);

                ChildJobPointDimensionListFragment childJobPointDimensionListFragment = new ChildJobPointDimensionListFragment();
                childJobPointDimensionListFragment.setArguments(bundle);
                pushFragment(childJobPointDimensionListFragment);
            } else {
                if (taskResponseBean != null) {
                    //从任务卡片进入
                    getJobPointType();
                } else if (functionResponseBean != null) {
                    //从功能卡片进入
                    Bundle bundle = new Bundle();

                    bundle.putString(KEY_BUSINESS_ID, businessId);
                    bundle.putBoolean(KEY_IS_PARENT, isParent);
                    bundle.putString(KEY_JOB_POINT_UKID, jobPointUkid);
                    bundle.putString(KEY_WAREHOUSE_UKID, warehouseUkid);
                    bundle.putString(KEY_WAREHOUSE_NAME, warehouseName);

                    ChooseChildJobPointTypeFragment chooseChildJobPointTypeFragment = new ChooseChildJobPointTypeFragment();
                    chooseChildJobPointTypeFragment.setArguments(bundle);
                    pushFragment(chooseChildJobPointTypeFragment);
                }
            }
        } else if (id == R.id.rl_step3 && isStep3Userful) { //作业点关联设置。
            Bundle bundle = new Bundle();
            bundle.putString("jobPointUkid", jobPointUkid);
            ChooseConnectPersonFragment3 chooseConnectPersonFragment3 = new ChooseConnectPersonFragment3();
            chooseConnectPersonFragment3.setArguments(bundle);

            pushFragment(chooseConnectPersonFragment3);
        } else if (id == R.id.rl_step4 && isStep4Userful) {
            if (isParent) {
                Bundle bundle = new Bundle();
                bundle.putString("warehouseUkid", warehouseUkid);
                bundle.putString("jobPointUkid", jobPointUkid);
                ChooseJobUnitLookmesspagerFragment4 chooseConnectObjectFragment4 = new ChooseJobUnitLookmesspagerFragment4();
                chooseConnectObjectFragment4.setArguments(bundle);
                pushFragment(chooseConnectObjectFragment4);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("jobPointUkid", jobPointUkid);
                bundle.putString("buId", businessId);
                ChooseConnectBluetoothFragment fragment = new ChooseConnectBluetoothFragment();
                fragment.setArguments(bundle);
                pushFragment(fragment);
            }
        } else if (id == R.id.rl_step5 && isStep5Userful) {
            //先请求数据，如果是有数据的直接到预览界面，没有数据到添加页面
            step5Request();
        } else if (id == R.id.rl_step6 && isStep6Userful) {
            Bundle bundle = new Bundle();
            bundle.putString("jobPointUkid", jobPointUkid);
            bundle.putString("jobPointUkid", jobPointUkid);
            ChooseConnectObjectFragment6 fragment6 = new ChooseConnectObjectFragment6();
            fragment6.setArguments(bundle);
            pushFragment(fragment6);
        } else if (id == R.id.rl_step7 && isStep7Userful) {
            Bundle bundle = new Bundle();

            bundle.putString(KEY_BUSINESS_ID, businessId);
            bundle.putString(KEY_JOB_POINT_UKID, jobPointUkid);

            SetJobPointRuleFragment setJobPointRuleFragment = new SetJobPointRuleFragment();
            setJobPointRuleFragment.setArguments(bundle);
            pushFragment(setJobPointRuleFragment);
        }
    }

    public void onEventMainThread(TaskPointCardEvent event) {
        if (event.isFinish1()) {
            restartAll();
            setStep1Enable(DONE);
            setStep2Enable(USERFUL);

            if (event.getObject() instanceof JobSaveConstant.Step1) {
                JobSaveConstant.Step1 step1 = (JobSaveConstant.Step1) event.getObject();
                mJobSaveConstant.setStep1(step1);
            }
        } else if (event.isFinish2()) {
            restartAll();
            setStep1Enable(DONE);
            setStep2Enable(DONE);
            setStep3Enable(USERFUL);
        } else if (event.isFinish3()) {
            restartAll();
            setStep1Enable(DONE);
            setStep2Enable(DONE);
            setStep3Enable(DONE);
            setStep4Enable(USERFUL);

            //保存第三步返回的数据

        } else if (event.isFinish4()) {
            restartAll();
            setStep1Enable(DONE);
            setStep2Enable(DONE);
            setStep3Enable(DONE);
            setStep4Enable(DONE);
            setStep5Enable(USERFUL);

            //保存第四步返回的数据
            if (event.getObject() instanceof JobSaveConstant.Step4) {
                JobSaveConstant.Step4 step4 = (JobSaveConstant.Step4) event.getObject();
                mJobSaveConstant.setStep4(step4);
            }

        } else if (event.isFinish5()) {
            restartAll();
            setStep1Enable(DONE);
            setStep2Enable(DONE);
            setStep3Enable(DONE);
            setStep4Enable(DONE);
            setStep5Enable(DONE);
            setStep6Enable(USERFUL);
        } else if (event.isFinish6()) {
            // 第六步 必选
            setStep1Enable(DONE);
            setStep2Enable(DONE);
            setStep3Enable(DONE);
            setStep4Enable(DONE);
            setStep5Enable(DONE);
            setStep6Enable(DONE);
            setStep7Enable(USERFUL);
        } else if (event.isFinish7()) {
            // 第七步 可选
            setStep1Enable(DONE);
            setStep2Enable(DONE);
            setStep3Enable(DONE);
            setStep4Enable(DONE);
            setStep5Enable(DONE);
            setStep6Enable(DONE);
            setStep7Enable(DONE);

            isStepAllDone = true;
        }
    }

    public void onEventMainThread(TaskPointBackEvent event) {
        jobPointType = event.getJobPointType();
        isFromCard = event.isFromCard();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BaseApplication.sp.putBooleanValue("isCanfinish", false);
        EventBus.getDefault().unregister(this);
    }

    /**
     * 回复所有的状态
     */
    public void restartAll() {
        setStep1Enable(USELESS);
        setStep2Enable(USELESS);
        setStep3Enable(USELESS);
        setStep4Enable(USELESS);
        setStep5Enable(USELESS);
        setStep6Enable(USELESS);
        setStep7Enable(USELESS);
    }

    /**
     * 按钮及其背景的控制
     * -1 0 1 三种状态
     *
     * @param enable
     */

    public void setStep1Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep1Userful = true;
            mIvStep1.setImageResource(R.drawable.task_position_white1);
            mRlStep1.setBackgroundResource(R.drawable.common_card_background_c1_shape);
            mTvStep1.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes1.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//不可用 未编辑
            isStep1Userful = false;
            mIvStep1.setImageResource(R.drawable.task_position_gray1);
            mRlStep1.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep1.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes1.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep1Userful = false;
            mIvStep1.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep1.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep1.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes1.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }

    /**
     * 按钮及其背景的控制
     * icon_task_addpoint_done
     *
     * @param enable
     */
    public void setStep2Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep2Userful = true;
            mIvStep2.setImageResource(R.drawable.task_position_white2);
            mRlStep2.setBackgroundResource(R.drawable.common_card_background_c1_shape);
            mTvStep2.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes2.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//可用 未编辑
            isStep2Userful = false;
            mIvStep2.setImageResource(R.drawable.task_position_gray2);
            mRlStep2.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep2.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes2.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep2Userful = false;
            mIvStep2.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep2.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep2.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes2.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }

    /**
     * 按钮及其背景的控制
     * icon_task_addpoint_done
     *
     * @param enable
     */
    public void setStep3Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep3Userful = true;
            mIvStep3.setImageResource(R.drawable.task_position_white3);
            mRlStep3.setBackgroundResource(R.drawable.common_card_background_c12_shape);
            mTvStep3.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes3.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//不可用 未编辑
            isStep3Userful = false;
            mIvStep3.setImageResource(R.drawable.task_position_gray3);
            mRlStep3.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep3.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes3.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep3Userful = false;
            mIvStep3.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep3.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep3.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes3.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }

    /**
     * 按钮及其背景的控制
     * icon_task_addpoint_done
     *
     * @param enable
     */
    public void setStep4Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep4Userful = true;
            mIvStep4.setImageResource(R.drawable.task_position_white4);
            mRlStep4.setBackgroundResource(R.drawable.common_card_background_c1_shape);
            mTvStep4.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes4.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//不可用 未编辑
            isStep4Userful = false;
            mIvStep4.setImageResource(R.drawable.task_position_gray4);
            mRlStep4.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep4.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes4.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep4Userful = false;
            mIvStep4.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep4.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep4.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes4.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }

    /**
     * 按钮及其背景的控制
     *
     * @param enable
     */
    public void setStep5Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep5Userful = true;
            mIvStep5.setImageResource(R.drawable.task_position_white5);
            mRlStep5.setBackgroundResource(R.drawable.common_card_background_c13_shape);
            mTvStep5.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes5.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//不可用 未编辑
            isStep5Userful = false;
            mIvStep5.setImageResource(R.drawable.task_position_gray5);
            mRlStep5.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep5.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes5.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep5Userful = false;
            mIvStep5.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep5.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep5.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes5.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }

    /**
     * 按钮及其背景的控制
     *
     * @param enable
     */
    public void setStep6Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep6Userful = true;
            mIvStep6.setImageResource(R.drawable.task_position_white6);
            mRlStep6.setBackgroundResource(R.drawable.common_card_background_c13_shape);
            mTvStep6.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes6.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//不可用 未编辑
            isStep6Userful = false;
            mIvStep6.setImageResource(R.drawable.task_position_gray6);
            mRlStep6.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep6.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes6.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep6Userful = false;
            mIvStep6.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep6.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep6.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes6.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }

    /**
     * 按钮及其背景的控制
     *
     * @param enable
     */
    public void setStep7Enable(int enable) {
        if (enable == USERFUL) {//可用
            isStep7Userful = true;
            mIvStep7.setImageResource(R.drawable.task_position_white7);
            mRlStep7.setBackgroundResource(R.drawable.common_card_background_c13_shape);
            mTvStep7.setTextColor(getResources().getColor(R.color.white));
            mTvStepDes7.setTextColor(getResources().getColor(R.color.white));
        } else if (enable == USELESS) {//不可用 未编辑
            isStep7Userful = false;
            mIvStep7.setImageResource(R.drawable.task_position_gray7);
            mRlStep7.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep7.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes7.setTextColor(getResources().getColor(R.color.bfc1c4));
        } else if (enable == DONE) {
            isStep7Userful = false;
            mIvStep7.setImageResource(R.drawable.icon_task_addpoint_done);
            mRlStep7.setBackgroundResource(R.drawable.common_card_background_fff_shape);
            mTvStep7.setTextColor(getResources().getColor(R.color.bfc1c4));
            mTvStepDes7.setTextColor(getResources().getColor(R.color.bfc1c4));
        }
    }


    /**
     * 第五步 开始请求一次
     */
    public void step5Request() {
        Map<String, Object> map = new HashMap<>();
        /**
         * jobPointUkid		number	@mock=100000110
         */
        map.put("jobPointUkid", jobPointUkid);
        NoHttpUtils.httpPost(TaskCenterConstant.URL_ADD_JOBPOINT_STEP5_GETALLBYNAME, map, myResponse, REQUEST_STEP5);
        showProgressDialog();
    }

    /**
     * 启用作业点
     */
    public void startJobPoint() {
        Map<String, String> map = new HashMap<>();
        map.put("jobPointUkid", jobPointUkid);
        NoHttpUtils.httpPost(TaskCenterConstant.URL_ADD_JOBPOINT_ALLDONE, map, myResponse, REQUEST_ALL_JOBPOINT);
        showProgressDialog();
    }

    /**
     * 继续上次的步骤
     */
    public void getBeforeStep() {
        Map<String, String> map = new HashMap<>();
        map.put("operationUkid", "" + operationUkid);
        NoHttpUtils.httpPost(TaskCenterConstant.URL_ADD_JOBPOIONT_GETBEFORE, map, myResponse, REQUEST_ALL_GETBEFORE);
    }

    NoHttpUtils.OnResponseListener myResponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            switch (i) {
                case REQUEST_STEP5:
                    if ("0".equals(commonClass.getCode())) {
                        ArrayList<AddJobPointStep5HavedUnitResponseBean> beans = (ArrayList) JSON.parseArray(commonClass.getData().toString(), AddJobPointStep5HavedUnitResponseBean.class);
                        if (beans != null && beans.size() != 0) {//已经有了共享单位
                            Bundle bundle = new Bundle();
                            bundle.putString("jobPointUkid", jobPointUkid);
                            bundle.putString("warehouseUkid", warehouseUkid);
                            ChooseJobUnitHavedUnitPagerFragment5 protectTaskUnitFragment = new ChooseJobUnitHavedUnitPagerFragment5();
                            protectTaskUnitFragment.setArguments(bundle);
                            pushFragment(protectTaskUnitFragment);
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString("jobPointUkid", jobPointUkid);
                            bundle.putString("warehouseUkid", warehouseUkid);
                            ChooseJobUnitAddFirstpageFragment5 protectTaskUnitFragment = new ChooseJobUnitAddFirstpageFragment5();
                            protectTaskUnitFragment.setArguments(bundle);
                            pushFragment(protectTaskUnitFragment);
                        }
                    }
                    break;
                case REQUEST_ALL_JOBPOINT:
                    if ("0".equals(commonClass.getCode())) {
                        EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
                    } else {
                        toast("" + commonClass.getMsg());
                    }
                    break;

                case REQUEST_ALL_GETBEFORE:
                    if ("0".equals(commonClass.getCode())) {
                        beforeServerBean = JSON.parseObject(commonClass.getData().toString(), AddJobPointStepGetbeforeResponseBean.class);
                        LogUtils.showErrLog("任务返回的数据" + commonClass.getData().toString());

                        if (beforeServerBean == null && "0".equals(beforeServerBean.getStep())) { //上次没有任务

                        } else { //已经保存过了
                            isParent = ("0".equals(beforeServerBean.getParentJobPointUkid()) ? true : false);  // 0代表父

                            int step = 0;
                            try {
                                step = Integer.parseInt(beforeServerBean.getStep());
                            } catch (NumberFormatException e) {
                            }
                            switch (step) {
                                case 1:
                                    restartAll();
                                    setStep1Enable(DONE);
                                    setStep2Enable(USERFUL);

                                    // 参数重新赋值
                                    businessId = beforeServerBean.getBusinessUnitId();
                                    jobPointUkid = beforeServerBean.getJobPointUkid();
                                    warehouseUkid = beforeServerBean.getStockUkid();
                                    warehouseName = beforeServerBean.getStockName();

                                    break;
                                case 2:
                                    restartAll();
                                    setStep1Enable(DONE);
                                    setStep2Enable(DONE);
                                    setStep3Enable(USERFUL);

                                    jobPointUkid = beforeServerBean.getJobPointUkid();

                                    break;
                                case 3:
                                    restartAll();
                                    setStep1Enable(DONE);
                                    setStep2Enable(DONE);
                                    setStep3Enable(DONE);
                                    setStep4Enable(USERFUL);
                                    warehouseUkid = beforeServerBean.getStockUkid();
                                    BaseApplication.sp.putValue("warehouseUkid", warehouseUkid);
                                    jobPointUkid = beforeServerBean.getJobPointUkid();
                                    businessId = beforeServerBean.getBusinessUnitId();
                                    break;
                                case 4:
                                    restartAll();
                                    setStep1Enable(DONE);
                                    setStep2Enable(DONE);
                                    setStep3Enable(DONE);
                                    setStep4Enable(DONE);
                                    setStep5Enable(USERFUL);
                                    warehouseUkid = beforeServerBean.getStockUkid();
                                    BaseApplication.sp.putValue("warehouseUkid", warehouseUkid);
                                    jobPointUkid = beforeServerBean.getJobPointUkid();
                                    break;
                                case 5:
                                    restartAll();
                                    setStep1Enable(DONE);
                                    setStep2Enable(DONE);
                                    setStep3Enable(DONE);
                                    setStep4Enable(DONE);
                                    setStep5Enable(DONE);
                                    setStep6Enable(USERFUL);
                                    jobPointUkid = beforeServerBean.getJobPointUkid();
                                    break;
                                case 6:
                                    restartAll();
                                    setStep1Enable(DONE);
                                    setStep2Enable(DONE);
                                    setStep3Enable(DONE);
                                    setStep4Enable(DONE);
                                    setStep5Enable(DONE);
                                    setStep6Enable(DONE);
                                    setStep7Enable(USERFUL);
                                    jobPointUkid = beforeServerBean.getJobPointUkid();
                                    break;
                                case 7:
                                    setStep1Enable(DONE);
                                    setStep2Enable(DONE);
                                    setStep3Enable(DONE);
                                    setStep4Enable(DONE);
                                    setStep5Enable(DONE);
                                    setStep6Enable(DONE);
                                    setStep7Enable(DONE);
                                    ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);
                                    break;
                                default:
                                    //都当做是重新开始来做吧
                                    restartAll();
                                    setStep1Enable(USERFUL);
                                    break;
                            }
                        }
                    } else {
                        toast("" + commonClass.getMsg());
                        setStep1Enable(USERFUL);
                    }
                    break;
                case REQUEST_GET_JOB_POINT_TYPE:
                    if ("0".equals(commonClass.getCode())) {
                        String jobPointTypeId = beforeServerBean.getJbTypeId();

                        if (jobPointTypeId == null) {
                            //代表还没有开始做第2步， 这个时候要选择作业点类型
                            Bundle bundle = new Bundle();

                            bundle.putString(KEY_BUSINESS_ID, businessId);
                            bundle.putBoolean(KEY_IS_PARENT, isParent);
                            bundle.putString(KEY_JOB_POINT_UKID, jobPointUkid);
                            bundle.putString(KEY_WAREHOUSE_UKID, warehouseUkid);
                            bundle.putString(KEY_WAREHOUSE_NAME, warehouseName);

                            ChooseChildJobPointTypeFragment chooseChildJobPointTypeFragment = new ChooseChildJobPointTypeFragment();
                            chooseChildJobPointTypeFragment.setArguments(bundle);
                            pushFragment(chooseChildJobPointTypeFragment);
                        } else {
                            //代表已经开始做第2步，只不过还没有做完，这个时候不用选择作业点类型，直接跳转维度列表
                            List<JobPointTypeResponseBean> jobPointTypeList = JSON.parseArray(commonClass.getData().toString(), JobPointTypeResponseBean.class);

                            for (JobPointTypeResponseBean jobPointTypeResponseBean : jobPointTypeList) {
                                if (jobPointTypeId.equals(jobPointTypeResponseBean.getJbTypeUkid())) {
                                    jobPointType = jobPointTypeResponseBean.getJbTypeName();
                                }
                            }

                            Bundle bundle = new Bundle();

                            bundle.putString(KEY_BUSINESS_ID, businessId);
                            bundle.putBoolean(KEY_IS_PARENT, isParent);
                            bundle.putBoolean(KEY_IS_FROM_CARD, true);
                            bundle.putString(KEY_JOB_POINT_UKID, jobPointUkid);
                            bundle.putString(KEY_JOB_POINT_TYPE, jobPointType);
                            bundle.putString(KEY_WAREHOUSE_UKID, warehouseUkid);
                            bundle.putString(KEY_WAREHOUSE_NAME, warehouseName);

                            ChildJobPointDimensionListFragment childJobPointDimensionListFragment = new ChildJobPointDimensionListFragment();
                            childJobPointDimensionListFragment.setArguments(bundle);
                            pushFragment(childJobPointDimensionListFragment);
                        }
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }

        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();

            dismissProgressDialog();
        }
    };

    private void getJobPointType() {
        showProgressDialog();

        Map map = new HashMap();

        map.put("buId", businessId);

        if (isParent) {
            map.put("type", "1");
        } else {
            map.put("type", "2");  //子作业点
        }

        NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_JOB_POINT_TYPE, map, myResponse, REQUEST_GET_JOB_POINT_TYPE);
    }
}
